package com.lxmes.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.lxmes.base.domain.NcFieldDict;
import com.lxmes.base.service.INcFieldDictService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 字段下拉定义Controller
 *
 * @author lx
 * @date 2023-04-18
 */
@Api(tags = "字段下拉定义")
@RestController
@RequestMapping("/base/fieldDict")
public class NcFieldDictController extends BaseController
{
    @Autowired
    private INcFieldDictService ncFieldDictService;

    /**
     * 查询字段下拉定义列表
     */
    @ApiOperation("查询字段下拉定义列表")
    @PreAuthorize("@ss.hasPermi('base:fieldDict:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcFieldDict ncFieldDict)
    {
        startPage();
        List<NcFieldDict> list = ncFieldDictService.selectNcFieldDictList(ncFieldDict);
        return getDataTable(list);
    }

    /**
     * 导出字段下拉定义列表
     */
    @ApiOperation("导出字段下拉定义列表")
    @PreAuthorize("@ss.hasPermi('base:fieldDict:export')")
    @Log(title = "字段下拉定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcFieldDict ncFieldDict)
    {
        List<NcFieldDict> list = ncFieldDictService.selectNcFieldDictList(ncFieldDict);
        ExcelUtil<NcFieldDict> util = new ExcelUtil<NcFieldDict>(NcFieldDict.class);
        util.exportExcel(response, list, "字段下拉定义数据");
    }

    /**
     * 获取字段下拉定义详细信息
     */
    @ApiOperation("获取字段下拉定义详细信息")
    @PreAuthorize("@ss.hasPermi('base:fieldDict:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncFieldDictService.selectNcFieldDictById(id));
    }

    /**
     * 新增字段下拉定义
     */
    @ApiOperation("新增字段下拉定义")
    @PreAuthorize("@ss.hasPermi('base:fieldDict:add')")
    @Log(title = "字段下拉定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcFieldDict ncFieldDict)
    {
        return toAjax(ncFieldDictService.insertNcFieldDict(ncFieldDict));
    }

    /**
     * 修改字段下拉定义
     */
    @ApiOperation("修改字段下拉定义")
    @PreAuthorize("@ss.hasPermi('base:fieldDict:edit')")
    @Log(title = "字段下拉定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcFieldDict ncFieldDict)
    {
        return toAjax(ncFieldDictService.updateNcFieldDict(ncFieldDict));
    }

    /**
     * 删除字段下拉定义
     */
    @ApiOperation("删除字段下拉定义")
    @PreAuthorize("@ss.hasPermi('base:fieldDict:remove')")
    @Log(title = "字段下拉定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncFieldDictService.deleteNcFieldDictByIds(ids));
    }
    /**
     * 导入字段下拉定义
     */
    @Log(title = "字段下拉定义", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NcFieldDict> util = new ExcelUtil<>(NcFieldDict.class);
        List<NcFieldDict> ncFieldDictList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncFieldDictService.imporIteminfo(ncFieldDictList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcFieldDict> util = new ExcelUtil<>(NcFieldDict.class);
        util.importTemplateExcel(response,"字段下拉定义");
    }
}
